package com.twu.biblioteca.dispatcher;

import com.twu.biblioteca.controller.BibliotecaController;
import com.twu.biblioteca.exception.BookNotAvailableException;
import com.twu.biblioteca.model.BibliotecaUser;
import com.twu.biblioteca.view.InfoView;
import com.twu.biblioteca.view.general.LoginView;
import com.twu.biblioteca.view.book.AddView;
import com.twu.biblioteca.view.book.CheckoutView;
import com.twu.biblioteca.view.book.ListView;
import com.twu.biblioteca.view.book.ReturnView;
import com.twu.biblioteca.view.general.MenuView;
import com.twu.biblioteca.view.general.UnknownView;
import com.twu.biblioteca.view.movie.CheckoutMovieView;
import com.twu.biblioteca.view.movie.ListMovieView;

public class BibliotecaDispatcher {
    private boolean alive;
    private BibliotecaController controller;

    public BibliotecaDispatcher() {
        this.alive = true;
        this.controller = new BibliotecaController();
    }

    public String dispatch(String command) throws BookNotAvailableException {
        if (BibliotecaCommandResolver.isListCommand(command)) {
            return ListView.transformToString(controller.getBookList());
        }
        if (BibliotecaCommandResolver.isCheckoutCommand(command)) {
            String bookName = BibliotecaCommandResolver.resolveCheckoutBook(command);
            return CheckoutView.transformToString(controller.checkout(bookName));
        }
        if (BibliotecaCommandResolver.isReturnCommand(command)) {
            String bookName = BibliotecaCommandResolver.resolveReturnBook(command);
            return ReturnView.transformToString(controller.returnBook(bookName));
        }
        if (BibliotecaCommandResolver.isHelpCommand(command)) {
            return MenuView.transformToString();
        }
        if (BibliotecaCommandResolver.isAddCommand(command)) {
            String bookName = BibliotecaCommandResolver.resolveAddBook(command);
            controller.addBook(bookName);
            return AddView.transformToString();
        }
        if (BibliotecaCommandResolver.isListMovieCommand(command)){
            return ListMovieView.transformToString(controller.getMovieList());
        }
        if (BibliotecaCommandResolver.isCheckoutMovieCommand(command)){
            String movieName = BibliotecaCommandResolver.resolveCheckoutMovie(command);
            return CheckoutMovieView.transformToString(controller.checkoutMovie(movieName));
        }
        if (BibliotecaCommandResolver.isLoginCommand(command)){
            BibliotecaUser user = BibliotecaCommandResolver.resolveLoginUser(command);
            return LoginView.transformToString(controller.setLogin(user));
        }
        if (BibliotecaCommandResolver.isInfoCommand(command)){
            return InfoView.transformToString(controller.getUser());
        }
        else {
            return UnknownView.transformToString();
        }
    }

    public boolean isAlive() {
        return alive;
    }

    public void setAlive(boolean alive) {
        this.alive = alive;
    }

    public BibliotecaController getController() {
        return controller;
    }

    public void setController(BibliotecaController controller) {
        this.controller = controller;
    }
}
